<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\shop\model\ShippingModel;
use cmf\controller\BaseController;
use think\Db;


class TimingController extends BaseController
{
    /**
     * 定时任务
     * @adminMenu(
     *     'name'   => '定时任务',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '定时任务',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        echo "定时任务";
    }

    /**
     * 待处理任务
     * @adminMenu(
     *     'name'   => '待处理任务',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '待处理任务',
     *     'param'  => ''
     * )
     */
    public function to_doing()
    {
        //待发货
        $where['order_status']=1;
        $count_order['fahuo']=Db::name('order')->where($where)->count();
        //退换货待处理
        $where['order_status']=5;
        $count_order['tuihuanhuo']=Db::name('order')->where($where)->count();
      
       //提现处理
        $put_where['status']=0;
        $put_balance=Db::name('put_balance')->where($put_where)->count();
      
        //用户待发货【退换货】您有N订单，商家已同意退换货
        $where['order_status']=6;
        $tuihuanhuo=Db::name('order')->where($where)->group('buyer_id')->select();
        foreach($tuihuanhuo as $k=>$v){
            $data['order_status']=6;
            $data['buyer_id']=$v['buyer_id'];
            $count=Db::name('order')->where($where)->count();
            $user_info=Db::name('user')->find($v['buyer_id']);
            $param['code']=$count;
            $param['mobile']=$user_info['mobile'];
            $param['TemplateCode']='SMS_139226806';
            hook_one("send_mobile_verification_code", $param);
        }
        //卖家待发货【换货】
        $map['order_status']=9;
        $count_order['tuihuanhuo']=Db::name('order')->where($where)->count();
        $message="今日商城任务：待发货订单：".$count_order['fahuo']."，退换货待处理订单:".$count_order['tuihuanhuo']."，【换货】卖家待发货订单:".$count_order['tuihuanhuo']."，提现待处理：".$put_balance."。";
        $subject='今日商城任务';
        $site_info=cmf_get_option('site_info');
        $result  = cmf_send_email($site_info['site_admin_email'], $subject, $message);

        if ($result['error']) {
            file_put_contents('log/error.txt',var_export($result,true),FILE_APPEND);
        }
    }


    /**
     * 定时收货
     * @adminMenu(
     *     'name'   => '定时收货',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '定时收货5分钟执行一次',
     *     'param'  => ''
     * )
     */
    public function get_order()
    {
        $time=time();
        $seven_time=$time-7*60*60*24;
        //发货七天收货
        $where['order_status']=2;
        $where['consign_time']=['<',$seven_time];
        $order_list=Db::name('order')->where($where)->select();
        foreach ($order_list as $k=>$v){
            $data['order_status']=3;
            $data['get_time']=time();
            //更新收货
            Db::name('order')->where('order_id',$v['order_id'])->update($data);
        }

        //退换货，用户发货后，七天商家收货
        $where1['order_status']=8;
        $where1['refund_userfahuo_time']=['<',$seven_time];
        $order_list1=Db::name('order')->where($where1)->select();
        foreach ($order_list1 as $k=>$v){
            $data['order_status']=9;
            $data['refund_storeget_time']=time();
            //更新收货
            Db::name('order')->where('order_id',$v['order_id'])->update($data);
        }
        //退换货，商家发货后，七天用户收货
        $where2['order_status']=10;
        $where2['refund_storefahuo_time']=['<',$seven_time];
        $order_list2=Db::name('order')->where($where2)->select();
        foreach ($order_list2 as $k=>$v){
            $data['order_status']=3;
            $data['refund_userget_time']=time();
            //更新收货
            Db::name('order')->where('order_id',$v['order_id'])->update($data);
        }

    }
    /**
     * 定时更新物流
     * @adminMenu(
     *     'name'   => '定时更新物流',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '定时更新物流每天更新一次',
     *     'param'  => ''
     * )
     */
    public function update_shipping()
    {

        //定时更新物流
        $where['order_status']=2;
        $order_list=Db::name('order')->where($where)->select();
        foreach ($order_list as $k=>$v){
            $shipping=new ShippingModel();
            $shipping->update_shipping($v['order_id']);
        }

    }

    /**
     * 定时完成（换货）订单+结算
     * @adminMenu(
     *     'name'   => '定时完成订单+结算',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '定时完成订单+结算 5分钟执行一次',
     *     'param'  => ''
     * )
     */
    public function order_finsh()
    {
        $time=time();
        $seven_time=$time-7*60*60*24;
        //收货后七天
        $where['order_status']=3;
        $where['get_time']=['<',$seven_time];
        $order_list=Db::name('order')->where($where)->select()->toArray();
        //换货完成
        $where2['order_status']=11;
        $order_list2=Db::name('order')->where($where2)->select()->toArray();
		if($order_list2){
            if($order_list){
                $order_list=array_merge($order_list,$order_list2);
            }else{
                $order_list=$order_list2;
            }
        }
        //退货拒绝的
        $where3['order_status']=7;
        $order_list3=Db::name('order')->where($where3)->select()->toArray();
        if($order_list3){
            if($order_list){
                $order_list=array_merge($order_list,$order_list3);
            }else{
                $order_list=$order_list3;
            }
        }
        foreach ($order_list as $k=>$v){
            Db::startTrans();
            try{
                //完成订单
                $data['order_status']=4;
                $data['finish_time']=time();
                Db::name('order')->where('order_id',$v['order_id'])->update($data);
                //结算订单
                $store_info=Db::name('store')->find($v['shop_id']);
                //结算金额
                $money=$v['pay_money']*0.2;
                $user_info= Db::name('user')->find($store_info['user_id']);
                Db::name('user')->where('id',$store_info['user_id'])->setInc('balance', $money);

                //添加记录
                $balance_info['user_id']=$store_info['user_id'];
                $balance_info['create_time']=time();
                $balance_info['change']=$money;
                $balance_info['balance']=$user_info['balance']+$money;
                $balance_info['description']='订单：'.$v['order_no'].'收入20%';
                $balance_info['remark']=json_encode($v);
                Db::name('user_balance_log')->insert($balance_info);

                if($store_info['rec_user_id']){
                    //推荐人获利
                    $tjmoney=$v['pay_money']*0.05;
                    $tj_user_info= Db::name('user')->find($store_info['rec_user_id']);
                    Db::name('user')->where('id',$store_info['rec_user_id'])->setInc('balance', $tjmoney);

                    //添加记录
                    $balance_info['user_id']=$store_info['rec_user_id'];
                    $balance_info['create_time']=time();
                    $balance_info['change']=$tjmoney;
                    $balance_info['balance']=$tj_user_info['balance']+$tjmoney;
                    $balance_info['description']='订单：'.$v['order_no'].'收入5%';
                    $balance_info['remark']=json_encode($v);
                    Db::name('user_balance_log')->insert($balance_info);
                }
                $data2['order_status']=13;
                Db::name('order')->where('order_id',$v['order_id'])->update($data2);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                file_put_contents('log/error.txt',var_export($e,true),FILE_APPEND);
                Db::rollback();
            }

        }
    }


    /**
     * 退货订单结算
     * @adminMenu(
     *     'name'   => '退货订单结算',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '退货订单结算 5分钟执行一次',
     *     'param'  => ''
     * )
     */
    public function order_finsh2()
    {
        //退货完成
        $where['order_status']=12;
        $order_list=Db::name('order')->where($where)->select();

        foreach ($order_list as $k=>$v){
            Db::startTrans();
            try{
                //完成订单
                $data['order_status']=4;
                $data['finish_time']=time();
                Db::name('order')->where('order_id',$v['order_id'])->update($data);
                //结算订单
                $store_info=Db::name('store')->find($v['shop_id']);
                //结算金额
                $money=($v['pay_money']-$v['refund_require_money'])*0.2;
                if($money>0){
                    $user_info= Db::name('user')->find($store_info['user_id']);
                    Db::name('user')->where('id',$store_info['user_id'])->setInc('balance', $money);

                    //添加记录
                    $balance_info['user_id']=$store_info['user_id'];
                    $balance_info['create_time']=time();
                    $balance_info['change']=$money;
                    $balance_info['balance']=$user_info['balance']+$money;
                    $balance_info['description']='订单：'.$v['order_no'].'收入20%';
                    $balance_info['remark']=json_encode($v);
                    Db::name('user_balance_log')->insert($balance_info);

                    if($store_info['rec_user_id']){
                        //推荐人获利
                        $tjmoney=($v['pay_money']-$v['refund_require_money'])*0.05;
                        $tj_user_info= Db::name('user')->find($store_info['rec_user_id']);
                        Db::name('user')->where('id',$store_info['rec_user_id'])->setInc('balance', $tjmoney);

                        //添加记录
                        $balance_info['user_id']=$store_info['rec_user_id'];
                        $balance_info['create_time']=time();
                        $balance_info['change']=$tjmoney;
                        $balance_info['balance']=$tj_user_info['balance']+$tjmoney;
                        $balance_info['description']='订单：'.$v['order_no'].'收入5%';
                        $balance_info['remark']=json_encode($v);
                        Db::name('user_balance_log')->insert($balance_info);
                    }
                }

                $data2['order_status']=13;
                Db::name('order')->where('order_id',$v['order_id'])->update($data2);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                file_put_contents('log/error.txt',var_export($e,true),FILE_APPEND);
                Db::rollback();
            }

        }
    }
  
   /**
     * 会员卡结算
     * @adminMenu(
     *     'name'   => '会员卡结算',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '会员卡结算',
     *     'param'  => ''
     * )
     */
    public function card()
    {

        $where['is_jiesuan']=0;
        $where['pay_status']=1;
        $order_list=Db::name('order_card')->where($where)->select();
        foreach ($order_list as $k=>$v){
           Db::startTrans();
            try{
                //店铺信息
                $store_info=Db::name('store')->find($v['store_id']);
                //店铺绑定用户信息
                $user_info= Db::name('user')->find($store_info['user_id']);
                //购买用户信息
                $buy_user_info= Db::name('user')->find($v['uid']);
                $tjmoney=0;
                if($buy_user_info['tj_user']>0){
                    //推荐人获利 6%
                    $tjmoney=$v['price']*0.06;
                    $tj_user_info= Db::name('user')->find($buy_user_info['tj_user']);
                    Db::name('user')->where('id',$buy_user_info['tj_user'])->setInc('balance', $tjmoney);

                    //添加记录
                    $balance_info['user_id']=$buy_user_info['tj_user'];
                    $balance_info['create_time']=time();
                    $balance_info['change']=$tjmoney;
                    $balance_info['balance']=$buy_user_info['balance']+$tjmoney;
                    $balance_info['description']='异业联盟推荐会员卡收入';
                    $balance_info['remark']=json_encode($v);
                    Db::name('user_balance_log')->insert($balance_info);
                }
              
                $money=$v['price']-$tjmoney;
                Db::name('user')->where('id',$store_info['user_id'])->setInc('balance', $money);

                //添加记录
                $balance_info['user_id']=$store_info['user_id'];
                $balance_info['create_time']=time();
                $balance_info['change']=$money;
                $balance_info['balance']=$user_info['balance']+$money;
                $balance_info['description']='会员卡收入';
                $balance_info['remark']=json_encode($v);
                Db::name('user_balance_log')->insert($balance_info);
              
                //更新订单
                $order_info['is_jiesuan']=1;
                Db::name('order_card')->where('id',$v['id'])->update($order_info);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                file_put_contents('log/error.txt',var_export($e,true),FILE_APPEND);
                Db::rollback();
            }
        }
    }
}